package com.afdor.rws.event.config;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Map;
import java.util.Properties;

/**
 * 配置信息
 *
 * @author 悭梵
 * @date Created in 2018-08-07 11:25
 */
@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class EventConfig implements Serializable {

    @Getter
    @Setter
    @NonNull
    private String name;

    private Properties properties = new Properties();

    public EventConfig(EventConfig config) {
        addAll(config);
    }

    public EventConfig(@NonNull Map<String, String> properties) {
        addAll(properties);
    }

    public Object setProperty(@NonNull String key, String value) {
        return properties.setProperty(key, value);
    }

    public String getProperty(@NonNull String key) {
        return properties.getProperty(key);
    }

    public String getProperty(@NonNull String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    public void addAll(@NonNull Map<String, String> properties) {
        if (properties != null && !properties.isEmpty()) {
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                setProperty(entry.getKey(), entry.getValue());
            }
        }
    }

    public void addAll(@NonNull EventConfig abstractConfig) {
        if (abstractConfig != null && !abstractConfig.properties.isEmpty()) {
            for (String key : abstractConfig.properties.stringPropertyNames()) {
                setProperty(key, abstractConfig.properties.getProperty(key));
            }
        }
    }

}
